<?php


namespace app\index\controller;

class App extends \app\common\Controller
{
	public function home($appid)
	{
		$app = \think\Db::name("app")->where(["id" => $appid])->find();
		if (empty($app)) {
			showMessageExit("应用ID不存在");
		}
		$this->assign("app", $app);
		$template = \app\system\service\AppService::get_home_template_config_data($app);
		$this->assign("config", $template["data"]["config"]);
		$ip = request()->ip();
		$ip_log = \think\Db::name("app_home_ip_log")->where(["appid" => $app["id"], "ip" => $ip])->find();
		list($start, $end) = \think\helper\Time::today();
		if (empty($ip_log)) {
			\app\common\server\StatisticsData::increases($app["id"], \app\common\model\AppData::AppHomeUv, 1);
			\think\Db::name("app_home_ip_log")->insert(["appid" => $app["id"], "ip" => $ip, "time" => time()]);
		} else {
			if ($ip_log["time"] < $start) {
				\app\common\server\StatisticsData::increases($app["id"], \app\common\model\AppData::AppHomeUv, 1);
				\think\Db::name("app_home_ip_log")->where(["id" => $ip_log["id"]])->update(["time" => \time()]);
			}
		}
		\app\common\server\StatisticsData::increases($app["id"], \app\common\model\AppData::AppHomePv, 1);
		return $this->fetch($template["data"]["view"]);
	}
	public function feedback_help()
	{
		$validate = \think\facade\Validate::make(["appid" => "require", "device_code" => "require", "device_info" => "require"], ["appid.require" => "应用ID 不能为空！", "device_code.require" => "设备码device_code 不能为空！", "device_info.require" => "设备信息device_info 不能为空！"]);
		if (!$validate->check($this->input)) {
			showMessageExit($validate->getError());
		}
		$appid = $this->input["appid"];
		$app = \think\Db::name("app")->where(["id" => $appid])->find();
		if (empty($app)) {
			showMessageExit("应用ID不存在");
		}
		$feedback_config = json_decode($app["feedback_config"], true);
		$template = $feedback_config["website_feedback_config"]["template"] ?? "default";
		$this->assign("appid", $appid);
		$this->assign("auth", $this->input["device_code"]);
		$this->assign("device_info", $this->input["device_info"]);
		return $this->fetch("./view/feedback/{$template}/index.html");
	}
	public function get_app_feedback_config($appid)
	{
		$app = \think\Db::name("app")->where(["id" => $appid])->find();
		if (empty($app)) {
			$this->response(\app\api\controller\Code::fail, "appid不存在");
		}
		$config = json_decode($app["feedback_config"], true);
		$config["helpList"] = $config["helpList"] ?? [];
		$this->response(\app\api\controller\Code::success, "获取成功", $config);
	}
	public function get_feedback_classify_list()
	{
		$app = \think\Db::name("app")->where(["id" => $this->input["appid"]])->find();
		if (empty($app)) {
			$this->response(\app\api\controller\Code::fail, "appid不存在");
		}
		$result = \app\api\service\AppService::get_feedback_classify_list($this->input);
		if ($result["code"] == \app\api\controller\Code::success) {
			$this->response($result["code"], "获取成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function get_feedback_data_list()
	{
		$app = \think\Db::name("app")->where(["id" => $this->input["appid"]])->find();
		if (empty($app)) {
			$this->response(\app\api\controller\Code::fail, "appid不存在");
		}
		$result = \app\api\service\AppService::get_feedback_data_list($this->input);
		if ($result["code"] == \app\api\controller\Code::success) {
			$this->response($result["code"], "获取成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function get_feedback_data_info()
	{
		$validate = \think\Validate::make(["feedback_id" => "require"], ["feedback_id.require" => "feedback_id 不能为空！"]);
		if (!$validate->check($this->input)) {
			return json(["code" => -1, "msg" => $validate->getError()]);
		}
		$app = \think\Db::name("app")->where(["id" => $this->input["appid"]])->find();
		if (empty($app)) {
			$this->response(\app\api\controller\Code::fail, "appid不存在");
		}
		$result = \app\api\service\AppService::get_feedback_data_info($this->input);
		if ($result["code"] == \app\api\controller\Code::success) {
			$this->response($result["code"], "获取成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function push_feedback_info()
	{
		$validate = \think\Validate::make(["content" => "require", "device_info" => "require", "device_code" => "require"], ["content.require" => "内容不能为空", "device_info.require" => "设备信息不能为空", "device_code.require" => "设备码不能为空"]);
		if (!$validate->check($this->input)) {
			return json(["code" => -1, "msg" => $validate->getError()]);
		}
		$app = \think\Db::name("app")->where(["id" => $this->input["appid"]])->find();
		if (empty($app)) {
			$this->response(\app\api\controller\Code::fail, "appid不存在");
		}
		$options = $this->input;
		$options["client_ip"] = request()->ip();
		$result = \app\api\service\AppService::push_feedback_info($options, "website");
		if ($result["code"] == \app\api\controller\Code::success) {
			$this->response($result["code"], "提交成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function feedback_file_update($appid)
	{
		$app = \think\Db::name("app")->where(["id" => $appid])->find();
		if (empty($app)) {
			$this->response(\app\api\controller\Code::fail, "appid不存在");
		}
		$feedback_config = json_decode($app["feedback_config"], true);
		if (empty($feedback_config["website_feedback_config"]["enable_feedback"])) {
			$this->response(\app\api\controller\Code::fail, "未开启在线反馈功能");
		}
		if (!empty($feedback_config["website_feedback_config"]["close_upload_file"])) {
			$this->response(\app\api\controller\Code::fail, "暂不支持上传反馈图片");
		}
		$upload_config = json_decode($app["upload_config"], true);
		$ip = request()->ip();
		$result = \app\api\service\CommonService::fileUpdate($upload_config, $ip);
		if ($result["code"] == \app\api\controller\Code::success) {
			$this->response($result["code"], $result["msg"], $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	private function response($code, $msg, $data = [], ...$other)
	{
		$response = ["code" => $code, "msg" => $msg, "data" => $data];
		foreach ($other as $array) {
			$response[$array[0]] = $array[1];
		}
		echo json_encode($response, JSON_UNESCAPED_UNICODE);
		exit;
	}
}